package cn.hnu.qq.frame;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import cn.hnu.qq.client.controller.FriendsListController;
import cn.hnu.qq.entity.message.DownLoadFileMessage;
import cn.hnu.qq.global.FileReceiver;

public class FileListDialog extends JDialog implements ActionListener {

	private final JPanel contentPanel = new JPanel();
	private JList list;
	private Object selected;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			FileListDialog dialog = new FileListDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public FileListDialog() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JLabel label = new JLabel("选择要下载的文件：");
			contentPanel.add(label, BorderLayout.NORTH);
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane, BorderLayout.CENTER);
			{
				JLabel label = new JLabel("文件名：");
				scrollPane.setColumnHeaderView(label);
			}
			{
				list = new JList();
				scrollPane.setViewportView(list);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("选择下载");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

	public void setFiels(List<String> files) {
		DefaultListModel<String> df = new DefaultListModel<String>();
		for(String f:files){
			df.addElement(f);
		}
		list.setModel(df);
		list.validate();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("OK")){
			String f = (String) list.getSelectedValue();
			if(f == null){
				JOptionPane.showMessageDialog(this, "no file selected");
				return;
			}else{
				this.dispose();
				JFileChooser jfc = new JFileChooser("./");
				int s = jfc.showSaveDialog(this);
				if(s == JFileChooser.APPROVE_OPTION){
					File file = jfc.getSelectedFile();
					FileReceiver fr = new FileReceiver(file.getAbsolutePath()+"\\");
					int port = 30000;
					while(!fr.bind(port)){
						port++;
					}
					
				}
				
			}
		}
		
	}
}
